// pages/movies/more_movies/more_movies.js
var ulit = require("../../../utils/util.js");
var app = getApp();
Page({
    data: {
        navigatDataTitle: "",
        movies: {},
        requstUrl: "",
        totalCount: 0,
        isEmpt: true
    },
    onLoad: function(options) {
        var options = options.categorys;
        this.data.navigatDataTitle = options;
        var dataUrl = "";
        switch (options) {
            case "正在热映":
                dataUrl = app.globlData.doubanbase + "/v2/movie/in_theaters";
                break;
            case "即将上映":
                dataUrl = app.globlData.doubanbase + "/v2/movie/coming_soon";
                break;
            case "top250":
                dataUrl = app.globlData.doubanbase + "/v2/movie/top250";
                break;
        }
        this.data.requstUrl = dataUrl
        ulit.http(dataUrl, this.movies_play);
    },
    onPullDownRefresh: function() {
        var refreshUrl = this.data.requstUrl + "?start=0&count=20";
        this.data.isEmpt = true;
        this.data.movies = {};
        this.data.totalCount = 0;
        ulit.http(refreshUrl, this.movies_play);
    },
    onReady: function() {
        //动态展示页面标题
        wx.setNavigationBarTitle({
            title: this.data.navigatDataTitle
        })
    },
    //下拉加载更多必须要使用这方法名，别的名字不会起作用
    onReachBottom: function() {
        console.log(this.data.totalCount)
        var nextUrl = this.data.requstUrl + "?start=" + this.data.totalCount + "&count=20";
        ulit.http(nextUrl, this.movies_play)
    },
    movies_play: function(data) {
        var movies = [];
        for (var i in data.subjects) {
            var subjects = data.subjects[i];
            var title = subjects.title;
            if (title.length >= 6) {
                title = title.substring(0, 6) + "...";
            }
            var temp = {
                stars: ulit.public_funtion(subjects.rating.stars),
                title: title,
                average: subjects.rating.average,
                images: subjects.images.large,
                id: subjects.id
            }
            movies.push(temp)
        }
        var moviesScroll = {};
        if (!this.data.isEmpt) {
            moviesScroll = this.data.movies.concat(movies);
        } else {
            moviesScroll = movies;
            this.data.isEmpt = false;
        }
        this.data.totalCount += 20;
        this.setData({
            movies: moviesScroll
        })
        wx.stopPullDownRefresh()
    },
    //进入详情页
    onDetail: function (event) {
        console.log(event)
        var moviesId = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../movies_detail/movies_detail?id=' + moviesId,
        })
    }
})